package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.PathUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.FileAdapter;
import flc.ast.adapter.RecordAdapter;
import flc.ast.bean.FileBean;
import flc.ast.databinding.ActivityRecordBinding;
import flc.ast.dialog.DeleteDialog;
import flc.ast.utils.Content;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.RxUtil;
import stark.common.basic.utils.UriUtil;

public class RecordActivity extends BaseAc<ActivityRecordBinding> {

    private RecordAdapter recordAdapter;
    private List<String> listSel = new ArrayList<>();
    private boolean isEdit = false;

    @Override
    protected int onCreate() {
        return R.layout.activity_record;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);

        mDataBinding.ivRecordBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.ivEdit.setOnClickListener(this);
        mDataBinding.tvAllSel.setOnClickListener(this);
        mDataBinding.tvDelete.setOnClickListener(this);

        mDataBinding.rvRecordList.setLayoutManager(new GridLayoutManager(mContext, 3));
        recordAdapter = new RecordAdapter();
        mDataBinding.rvRecordList.setAdapter(recordAdapter);
        recordAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivEdit://编辑
                isEdit = true;
                recordAdapter.setEdit(isEdit);
                recordAdapter.notifyDataSetChanged();
                mDataBinding.llEdit.setVisibility(View.VISIBLE);
                break;
            case R.id.tvAllSel://全选
                allSel();
                break;
            case R.id.tvDelete://删除
                if (listSel.size() == 0) {
                    ToastUtils.showShort(getString(R.string.please_sel_file));
                    return;
                }
                showDeleteDialog();
                break;
            case R.id.ivComplete://完成
                cancelEdit();
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (isEdit) {
            if (recordAdapter.getItem(position).isSelected()) {
                recordAdapter.getItem(position).setSelected(false);
                listSel.remove(recordAdapter.getItem(position).getPath());
            } else {
                recordAdapter.getItem(position).setSelected(true);
                listSel.add(recordAdapter.getItem(position).getPath());
            }
            recordAdapter.notifyItemChanged(position);
        } else {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(UriUtil.getVideoUri(mContext, recordAdapter.getItem(position).getPath()), "video/*");
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            startActivity(intent);
        }
    }

    @Override
    protected void initData() {
        getData();
    }

    private void getData() {
        List<FileBean> list = new ArrayList<>();
        //获取文件夹下文件
        String path = PathUtils.getFilesPathExternalFirst() + Content.myRecord;
        List<File> files = FileUtils.listFilesInDir(path);
        if (files.size() > 0) {
            for (File file : files) {
                list.add(new FileBean(file.getPath()));
            }
        }
        if (list.size() > 0) {
            recordAdapter.setList(list);
            mDataBinding.rvRecordList.setVisibility(View.VISIBLE);
            mDataBinding.ivRecordNoData.setVisibility(View.GONE);
        } else {
            mDataBinding.rvRecordList.setVisibility(View.GONE);
            mDataBinding.ivRecordNoData.setVisibility(View.VISIBLE);
        }
    }

    private void allSel() {
        listSel.clear();
        for (FileBean validDatum : recordAdapter.getValidData()) {
            listSel.add(validDatum.getPath());
            validDatum.setSelected(true);
        }
        recordAdapter.notifyDataSetChanged();
    }

    private void showDeleteDialog() {
        DeleteDialog deleteDialog = new DeleteDialog(mContext);
        deleteDialog.setListener(new DeleteDialog.Ilistener() {
            @Override
            public void clickDelete() {
                delete();
            }
        });
        deleteDialog.show();
    }

    private void delete() {
        showDialog(getString(R.string.delete_ing));
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                RxUtil.create(new RxUtil.Callback<String>() {

                    @Override
                    public void doBackground(ObservableEmitter<String> emitter) {
                        //从此文件夹删除
                        for (String s : listSel) {
                            FileUtils.delete(s);
                        }
                        emitter.onNext("");
                    }

                    @Override
                    public void accept(String path) {
                        dismissDialog();
                        ToastUtils.showShort(getString(R.string.delete_suc));
                        cancelEdit();
                    }
                });
            }
        }, 1000);
    }

    private void cancelEdit() {
        isEdit = false;
        recordAdapter.setEdit(false);
        getData();
        mDataBinding.llEdit.setVisibility(View.GONE);
    }

}